/*
* @description: 工具类
* @Author: 问道师兄木木木
* @Date: 2021-03-24 15:58:06
 * @LastEditors: 问道师兄木木木
 * @LastEditTime: 2021-06-02 11:54:33
*/

import _util from "@/types/sgs/lib.util"

class utilClass implements _util {

    /** 定时器的时间戳id */
    public timer: number = 0

    constructor() { }
    getStorage<T extends "token">(key: T): _util.getStorageOption[T] {
        throw new Error("Method not implemented.")
    }
    sizeFormat(num: number): number {
        throw new Error("Method not implemented.")
    }
    public isType = <T>(val: T) => {
        const _type: _util.isTypeOption = {
            string: "[object String]",
            number: "[object Number]",
            boolean: "[object Boolean]",
            undefined: "[object Undefined]",
            null: "[object Null]",
            object: "[object Object]",
            function: "[object Function]",
            array: "[object Array]",
            date: "[object Date]",
            regExp: "[object RegExp]"
        }
        for (const key in _type) { if (_type[key] == Object.prototype.toString.call(val)) return <keyof _util.isTypeOption>key }
        return <keyof _util.isTypeOption>"undefined"
    }

    public mapMutations<T>(_this: T, _key: Array<keyof typeof _this>) {
        let _obj: Partial<T> = {}
        _key.forEach((el, index) => _obj[el] = _this[_key[index]])
        return <Required<T>>_obj
    }

    public getEventData(e) {
        return e.currentTarget.dataset
    }

    public isEmptyObject(value) {
        return JSON.stringify(value) === "{}"
    }
    public isObjectExist(obj, key, _trip?) {
        return Object.prototype.hasOwnProperty.call(obj, key) && obj[key]
    }

    public isArrayUndefined(arr) {
        return !arr.some(() => 1 == 1)
    }

    public getSetDataKey(param) {
        return [undefined, null, -1].every(el => param.index !== el) ? `${param.listkey}[${param.index}]` : `${param.listkey}`
    }

    public getTimeComputed: _util['getTimeComputed'] = (ms) => {
        return {
            day: ms / (24 * 3600 * 1000),
            hours: ms / (3600 * 1000),
            min: ms / (60 * 1000),
            seconds: ms / 1000
        }
    }

    public parseNumberTwo(num, len = 2) {
        let numStr = num.toString()
        let index = numStr.indexOf('.')
        if (index == -1) {
            numStr = Number(num).toFixed(2).toString()
            index = numStr.indexOf('.')
        }
        let result = Number(numStr.slice(0, index + len + 1))
        return result.toString().indexOf('.') == -1 ? result + '.00' : result
    }

    public nFormatW(num) {
        return num > 9999.99 ? this.parseNumberTwo(num / 10000) + 'w' : this.parseNumberTwo(num).toString()
    }

    public debounce(param) {
        return (...args: any) => {
            if (param.immediate) {
                param.func.apply(this, args) // 确保引用函数的指向正确，并且函数的参数也不变
                param.immediate = false
                return
            }
            clearTimeout(this.timer)
            this.timer = setTimeout(() => {
                param.func.apply(this, args)
            }, param.delay)
        }
    }

    public throttle(param) {
        let prev = Date.now() - param.rateTime;
        return (...args: any[]) => {
            let remaining = param.rateTime - (Date.now() - prev);
            clearTimeout(this.timer);
            if (remaining <= 0) {
                param.fn.apply(this, args);
                prev = Date.now();
            } else {
                this.timer = setTimeout(() => {
                    param.fn.apply(this, args)
                }, remaining)
            }
        }
    }
}


export let sgs_util: _util = new utilClass()